package cn.gok.oa.system.controller;


import cn.gok.oa.log.LogAnnotation;
import cn.gok.oa.system.dto.PermissionDto;
import cn.gok.oa.system.dto.UserListDto;
import cn.gok.oa.system.entity.TbUser;
import cn.gok.oa.system.service.ITbMenuRoleService;
import cn.gok.oa.system.service.ITbUserService;
import cn.gok.oa.system.vo.UserPermissionVo;
import cn.gok.oa.util.ResultData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gok
 * @since 2022-08-04
 */
@RestController
@RequestMapping("/permissions")
public class TbMenuRoleController {

    @Autowired
    ITbMenuRoleService iTbMenuRoleService;

    @Autowired
    ITbUserService iTbUserService;

    @RequestMapping(value = "/getUserRoleList", method = RequestMethod.GET)
    @LogAnnotation("获取已匹配此权限的用户")
    @ResponseBody
    public ResultData getUserRoleList(UserListDto userListDto){
        Page<UserPermissionVo> page = new Page<>(userListDto.getPage(), userListDto.getLimit());
        IPage<UserPermissionVo> iPage = iTbMenuRoleService.getList(page, userListDto.getRoleId());
        return ResultData.page(iPage.getRecords(), iPage.getTotal());
    }

    @RequestMapping(value = "/getUserIsNotRoleList", method = RequestMethod.GET)
    @LogAnnotation("获取未匹配此权限的用户")
    @ResponseBody
    public ResultData getUserIsNotRoleList(UserListDto userListDto){
        Page<UserPermissionVo> page = new Page<>(userListDto.getPage(), userListDto.getLimit());
        IPage<UserPermissionVo> iPage = iTbMenuRoleService.getListNone(page, userListDto.getRoleId());
        return ResultData.page(iPage.getRecords(), iPage.getTotal());
    }

    @RequestMapping(value = "/allotUserPermission", method = RequestMethod.PUT)
    @LogAnnotation("分配用户权限")
    @ResponseBody
    public ResultData allotUserPermission(@RequestBody PermissionDto permissionDto, HttpServletRequest httpServletRequest){
        try {
            iTbMenuRoleService.allotUserPermission(permissionDto, httpServletRequest);
        } catch (RuntimeException e) {
            return ResultData.fail();
        } return ResultData.success();
    }

    @RequestMapping(value = "/cancelUserPermission", method = RequestMethod.PUT)
    @LogAnnotation("取消用户权限授权")
    @ResponseBody
    public ResultData cancelUserPermission(Integer userId, HttpServletRequest httpServletRequest){
        try {
            iTbMenuRoleService.cancelUserPermission(userId, httpServletRequest);
        } catch (RuntimeException e) {
            return ResultData.fail();
        } return ResultData.success();
    }

    @RequestMapping(value = "/cancelAllUserPermission", method = RequestMethod.PUT)
    @LogAnnotation("批量取消用户权限授权")
    @ResponseBody
    public ResultData cancelAllUserPermission(@RequestBody PermissionDto permissionDto, HttpServletRequest httpServletRequest){
        try {
            iTbMenuRoleService.cancelAllUserPermission(permissionDto, httpServletRequest);
        } catch (RuntimeException e) {
            return ResultData.fail();
        } return ResultData.success();
    }

}
